package com.roadmap.common;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.roadmap.exception.*;

/** this class contains error messages to be shown on the UI */
public abstract class RdcErrorMessage {
	private final static Logger _log = RdcLogger.getLogger(RdcErrorMessage.class);
	
	public final static String BAD_PARAMETERS = "输入的参数不正确";

	private final static String USER_NOT_LOGGED_IN = "您尚未登录";

	private final static String SYSTEM_EXCEPTION = "系统未能处理您的请求";

	private final static String ACTION_NOT_SUPPORTED = "不支持该操作";

	private final static String INVALID_STATUS = "对象的状态不合法";

	private final static String ENTITY_NOT_FOUND = "未找到指定对象";

	private final static String ACTION_INVALID = "操作不合法";

	private final static String ACTION_NOT_AUTHROIZED = "没有权限执行该操作";

	private final static Map<String, String> exceptionMsgMap = new HashMap<String, String>();

	static {
		exceptionMsgMap.put(NotLoggedOnException.class.getSimpleName(),
				USER_NOT_LOGGED_IN);
		exceptionMsgMap.put(EntityNotFoundException.class.getSimpleName(),
				ENTITY_NOT_FOUND);
		exceptionMsgMap.put(ActionNotSupportedException.class.getSimpleName(),
				ACTION_NOT_SUPPORTED);
		exceptionMsgMap.put(NullObjectException.class.getSimpleName(),
				BAD_PARAMETERS);
		exceptionMsgMap.put(InvalidActionException.class.getSimpleName(),
				ACTION_INVALID);
		exceptionMsgMap.put(NotAuthorizedException.class.getSimpleName(),
				ACTION_NOT_AUTHROIZED);
		exceptionMsgMap.put(InvalidStatusException.class.getSimpleName(),
				INVALID_STATUS);
	}

	/** get the message corresponding to the specific exception */
	public static String getMessageToClient(Exception e) {
		if (e == null) {
			return SYSTEM_EXCEPTION;
		}

		String msg = exceptionMsgMap.get(e.getClass().getSimpleName());
		if (msg == null) {
			msg = SYSTEM_EXCEPTION;
			
			// 记录未处理到的异常
			_log.error(e);
		}
		return msg;
	}
}
